from playwright.sync_api import sync_playwright


def extracePage(page):
    ret = []
    
    categoryElements = page.locator('.gridlist>ul>li>a').all()
    ret = [[item.text_content(), item.get_attribute('href')] for item in categoryElements]
    return ret
    


def write2dListToFile(data, filePath):
    with open(filePath, 'w', encoding='utf-8') as f:
        for inner_list in data:
            # 将内层列表元素转为字符串并用逗号连接
            line = ','.join(map(str, inner_list))
            f.write(line + '\n')


def getCategoryList():
    startUrl = 'https://www.meishichina.com/YuanLiao/gongxiao/'
    saveFile = './shiliao.txt'
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(
        headless=False,
        args=["--no-sandbox",
            "--disable-blink-features=AutomationControlled",
            "--disable-dev-shm-usage",
            "--disable-gpu"
        ]
    )

    context = browser.new_context(
        viewport={"width": 1366, "height": 768},
    )
    page = context.new_page()

    page.goto(startUrl)
    categoryList = extracePage(page)

    #write to text
    write2dListToFile(categoryList, saveFile)

    page.close()
    context.close()
    browser.close()
    playwright.stop()




def main():
    getCategoryList()

if __name__ == "__main__":
    main()

